package com.zdsoft.crm.controller.email;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/email")
public class EmailVerification {

	
	@Autowired
	JavaMailSender mailSender;
	
	private int Code;

	@GetMapping
	public ModelAndView send(String email) throws MessagingException {
//		 建立邮件消息
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
//		 设置信息
		messageHelper.setTo(email);// 接受email
		messageHelper.setFrom("2353942162@qq.com");// 发送email
		messageHelper.setSubject("京唐国际邮箱验证码"); // 标题
		int verificationCode = (int)(Math.random() * 10000);
		this.Code=verificationCode;
		messageHelper.setText("<html>"
							+ "      <br/>"
							+ "		【京唐国际】验证码：&nbsp;<h2>"+verificationCode+"</h2>"
							+ "     <br/><br/><br/><h5>&nbsp;&nbsp;&nbsp;京唐国际</h5>"
							+ "</html>", true);
		mailSender.send(mimeMessage);
		
		// --------
		
		ModelAndView mav=new ModelAndView("/email/email.html");
		
		
		
		
		return mav;
		
	}
	
	
	@PostMapping
	@ResponseBody
	public boolean verification(int verificationCode){
		
		
		
		
		
		
		
		return false;
	}
}
